package com.lagou.codec;

import com.lagou.pojo.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author zhouyp
 * @program code
 * @description
 * @create 2020-06-13
 */
public class RpcDecoder extends ByteToMessageDecoder {

	private final Serializer serializer;

	public RpcDecoder(Serializer serializer) {
		this.serializer = serializer;
	}


	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in,
	                      List<Object> out) throws Exception {
		final byte[] bytes = new byte[in.readableBytes()];
		in.readBytes(bytes);
		final RpcRequest request = serializer.deserialize(RpcRequest.class, bytes);
		out.add(request);
	}
}
